﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KAKA.Domain;
using Morus.Infrastructure;
using Morus;
using KAKA.Infrastructure;

namespace KAKA.Repository
{
    public interface IUserRepository : IRepository<User>
    {
        User Get(string username);
        User Get(Guid id);
        Proof GetProof(Guid token);
    }

    public class UserRepository : Repository<User>, IUserRepository
    {
        public UserRepository(Database database)
            : base(database)
        {
        }

        public User Get(string username)
        {
            Check.Argument.IsNotEmpty(username, "username");

            return base.Get<User>(Spec<User>.Eval(r => r.Username.ToLower() == username.ToLower()));
        }

        public User Get(Guid id)
        {
            Check.Argument.IsNotEmpty(id, "id");

            return base.Get<User>(Spec<User>.Eval(r => r.Id == id));
        }

        public Proof GetProof(Guid token)
        {
            var hashToken = Convert.ToBase64String(token.ToByteArray());
            return base.Get<Proof>(Spec<Proof>.Eval(r => r.Value == hashToken && r.Used == false));
        }
    }
}
